package com.android.camera;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

import android.app.ListActivity;
import android.content.ActivityNotFoundException;
import android.content.ContentResolver;
import android.content.Intent;
import android.content.res.AssetFileDescriptor;
import android.database.Cursor;
import android.media.ExifInterface;
import android.os.Bundle;
import android.provider.MediaStore.Images.Media;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;

import com.android.camera.gallery.IImage;
import com.android.camera.gallery.IImageList;
import com.android.camera.rephoto.R;

public class RephotoImageList extends ListActivity
{
    public static final String TAG="RephotoImageList";
    private ContentResolver mContentResolver;
    static final String LIST_IMAGES_INTENT = "android.intent.action.IMAGE_LIST";
    private List<ImageAndText> ImageTextList = new LinkedList<ImageAndText>();
    
    /* (non-Javadoc)
     * @see android.app.ListActivity#onListItemClick(android.widget.ListView, android.view.View, int, long)
     */
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        ImageAndText img=(ImageAndText)l.getAdapter().getItem(position);
        ContentResolver cr = getContentResolver ( );
        Log.d(TAG, "uri: "+img.getUri());
        
        String projectName=null;

        // TODO: Lookup the project name using the URI of the image
        // is that possible?

        Cursor pc = null;
        try {
            pc = cr.query ( img.getUri ( ), null, null, null, null );
            pc.moveToFirst ( );
            int i = pc.getColumnIndex ( Camera.PROJECT_NAME );
            projectName = pc.getString ( i );
            Log.v ( TAG, "Project name is: " + projectName );

            Intent intent = new Intent(Camera.REPHOTO_INTENT);
            // OK, not sure what this means
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            // Directly set bitmap as an "extra" to the intent

            intent.putExtra(Camera.TRANSPARENT_IMAGE_URI, img.getUri());
            intent.putExtra(Camera.PROJECT_NAME, projectName);
            intent.putExtra(RephotoProvider.IS_ORIGINAL, "1");
            this.startActivity(intent);
        } catch (ActivityNotFoundException e) {
            Log.e(TAG, "Could not start rephoto camera activity", e);
        } finally {
            if (pc!=null) {
                pc.close();
            }
        }
    }

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.image_list);
        
        mContentResolver=getContentResolver();
        
    }

	/* (non-Javadoc)
     * @see android.app.Activity#onCreateOptionsMenu(android.view.Menu)
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.list_options_menu, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.new_project:
            // Launch activity to insert a new item
            startActivity(new Intent(Intent.ACTION_INSERT, getIntent().getData()));
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }
    
    @Override
	protected void onStart() {
		
		// Could replace with a content resolver?
        IImageList imageList=ImageManager.makeImageList(mContentResolver,
                ImageManager.DataLocation.EXTERNAL,
                ImageManager.INCLUDE_IMAGES,
                ImageManager.SORT_ASCENDING,
                ImageManager.CAMERA_IMAGE_BUCKET_ID,
                true);
        
        //mContentResolver.que
        
        for (int i=0; i<imageList.getCount(); i++) {
            IImage image=imageList.getImageAt(i);
            ImageTextList.add(new ImageAndText(image.miniThumbBitmap(),
                    image.getProjectName(),
                    image.fullSizeImageUri()));
        }
        
        // Will use the toString() method of IImage, which just prints the
        // URI for the actual image
        // We need to get out information about the image and a bitmap of some kind
        
        setListAdapter(new ImageAndTextListAdapter(this, ImageTextList));
		
		super.onStart();
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		
		for ( ImageAndText iat : ImageTextList ) {
			
			iat.recycle ( );
			
		}
		
		ImageTextList.clear ( );
		
		super.onStop();
	}
}
